package com.ruoyi.cbs.contract.services;

import com.ruoyi.cbs.contract.domain.*;
import com.ruoyi.cbs.contract.domain.vo.*;
import com.ruoyi.cbs.contract.service.*;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContractorPersonnelServices {
    @Autowired
    private IContractorPersonnelService contractorPersonnelService;
    @Autowired
    private IContractorPersonnelCertService contractorPersonnelCertService;


    /**
     * 查询承包商人员列表
     * @return
     */
    public List<ContractorPersonnelSelectVo> selectContractorPersonnelList(String contractorId, String personName) {
        ContractorPersonnel searchEntity = new ContractorPersonnel();
        searchEntity.setContractorId(new Long(contractorId));
        searchEntity.setName(personName);
        List<ContractorPersonnel> personnelList = contractorPersonnelService.selectContractorPersonnelList(searchEntity);

        List<ContractorPersonnelSelectVo> personnelSelectVoList = new ArrayList<>();
        for (ContractorPersonnel contractorPersonnel : personnelList) {
            ContractorPersonnelSelectVo contractorPersonnelSelectVo = new ContractorPersonnelSelectVo();
            BeanUtils.copyProperties(contractorPersonnel, contractorPersonnelSelectVo);
            contractorPersonnelSelectVo.setPersonId(contractorPersonnel.getId());
            personnelSelectVoList.add(contractorPersonnelSelectVo);
        }
        return personnelSelectVoList;
    }

    /**
     * 查询承包商人员列表
     * @return
     */
    public List<ContractorPersonnelVo> selectListByContractorId(Long contractorId) {
        List<ContractorPersonnel> personnelList = contractorPersonnelService.selectListByContractorId(contractorId);

        // 初始化人员列表
        List<ContractorPersonnelVo> personnelVoList = new ArrayList<>();
        for (ContractorPersonnel personnel : personnelList) {
            ContractorPersonnelVo personnelVo = new ContractorPersonnelVo();
            BeanUtils.copyProperties(personnel, personnelVo);
            personnelVo.setPersonnelId(personnel.getId());
            personnelVoList.add(personnelVo);

            // 人员证书列表
            List<ContractorPersonnelCertVo> certVoList = selectListByPersonnelId(personnel.getId());
            personnelVo.setCertList(certVoList);
        }

        return personnelVoList;
    }

    /**
     * 查询承包商人员详情
     * @param personnelId
     * @return
     */
    public ContractorPersonnelVo getContractorPersonnelDetail(String personnelId) {
        ContractorPersonnelVo personnelVo = new ContractorPersonnelVo();
        // 承包商人员基础信息
        ContractorPersonnel contractorPersonnel = contractorPersonnelService.selectContractorPersonnelById(new Long(personnelId));
        BeanUtils.copyProperties(contractorPersonnel, personnelVo);
        personnelVo.setPersonnelId(contractorPersonnel.getId());
        personnelVo.setPersonName(contractorPersonnel.getName());
        personnelVo.setPersonType(contractorPersonnel.getPersonnelType());

        // 人员证书列表
        List<ContractorPersonnelCertVo> certVoList = selectListByPersonnelId(new Long(personnelId));
        personnelVo.setCertList(certVoList);

        return personnelVo;
    }

    /**
     * 查询人员证书列表
     * @param personnelId
     * @return
     */
    public List<ContractorPersonnelCertVo> selectListByPersonnelId(Long personnelId) {
        List<ContractorPersonnelCert> certList = contractorPersonnelCertService.selectListByPersonnelId(personnelId);
        List<ContractorPersonnelCertVo> certVoList = new ArrayList<>();
        for (ContractorPersonnelCert cert : certList) {
            ContractorPersonnelCertVo certVo = new ContractorPersonnelCertVo();
            BeanUtils.copyProperties(cert, certVo);
            certVo.setPersonnelCertId(cert.getId());
            certVoList.add(certVo);
        }

        return certVoList;
    }


    public boolean addContractorPersonnel(List<ContractorPersonnelVo> personnelVoList, Long contractorId, String contractorName) {
        for (ContractorPersonnelVo personnelVo : personnelVoList) {
            ContractorPersonnel personnel = new ContractorPersonnel();
            BeanUtils.copyProperties(personnelVo, personnel);
            personnel.setContractorId(contractorId);
            personnel.setContractorName(contractorName);
            personnel.setName(personnelVo.getPersonName());
            personnel.setBirthday(DateUtils.parseDate(personnelVo.getBirthday()));
            contractorPersonnelService.insertContractorPersonnel(personnel);

            // 承包商人员证书入库
            List<ContractorPersonnelCertVo> certVoList = personnelVo.getCertList();
            for (ContractorPersonnelCertVo certVo : certVoList) {
                ContractorPersonnelCert personnelCert = new ContractorPersonnelCert();
                BeanUtils.copyProperties(certVo, personnelCert);
                personnelCert.setContractorId(contractorId);
                contractorPersonnelCertService.insertContractorPersonnelCert(personnelCert);
            }
        }

        return true;
    }


}
